//判断app客户端
var ua = navigator.userAgent.toLowerCase();
var os = '';
if (/iphone|ipad|ipod/.test(ua)) {		   
	os = 'ios';
} else if (/android/.test(ua)) {		 
	os = 'android';
}

var informationid = getParameter("informationid");
var uid=getParameter("uid");
var record = 0;//总的点赞数
var issave = true;//是否保存
var hisclick = false;//历史是否点赞
$(function(){
	loadShowData();
	loadClickInfo();
	if(uid != null || uid == ""){
		$("#click").click(function(){
			if($("#flags").val() == 1 && issave && !hisclick){
				$("#flags").val("2");
				record = Number(record) + 1;
				$("#click").css("color","red");
				$("#clicklike").html(record);
				//保存
				saveclick();
			}else{
				moda("您已经点过赞了");
			}
			issave = false;
		});
	}else{
		return;
	}
});

function loadClickInfo(){
	var urlStr = serverPath + "/app/doctor/diabetes/clicklike.do"; 
	var jsonStr = 
    '{'+
    '	   "requestid":"",'+
    '	   "requestsource":"diabetes",'+
    '	   "sessionid":"",'+
    '	   "uid":"'+uid+'",'+
    '	   "method":"getclickcount",'+
    '	   "version":"1.0",'+
    '	   "requestlist":'+
    '	   ['+
    '	      {'+
    '            "infoid":"'+informationid+'"'+
    '	      }'+
    '	   ]'+
    '	}';
    UseAjax(urlStr, jsonStr, document.cookie, showClickInfo, '1');
}
function saveclick(){
	var urlStr = serverPath + "/app/doctor/diabetes/clicklike.do"; 
	var jsonStr = 
    '{'+
    '	   "requestid":"",'+
    '	   "requestsource":"diabetes",'+
    '	   "sessionid":"",'+
    '	   "uid":"'+uid+'",'+
    '	   "method":"savelikeinfo",'+
    '	   "version":"1.0",'+
    '	   "requestlist":'+
    '	   ['+
    '	      {'+
    '	         "islike":"1",'+
    '            "infoid":"'+informationid+'"'+
    '	      }'+
    '	   ]'+
    '	}';
    UseAjax(urlStr, jsonStr, document.cookie, showClickInfo, '1');
}
function showClickInfo(r){
	var wi;
	if(r.resultcode == 0){
		wi = r.resultlist[0].resultlist;
		record = wi.count;
		if(wi.islike == 1){
			hisclick = true;
			$("#click").css("color","red");
		}
	}
	$("#clicklike").html(record);
}

//加载展现数据
function loadShowData(){
	var urlStr = serverPath + "/app/doctor/diabetes/info.do";
	var jsonStr = '{method:"getMessageH",requestlist:[{ informationid:"'+informationid+'" }],  version:"1.0",uid:"'+uid+'", requestsource:"diabetes"}' ;
	UseAjax(urlStr, jsonStr, document.cookie, test, '1');
}
function test(nodeArrays) {
	var nodeArray = nodeArrays.resultlist[0].resultlist;
	if (nodeArray && nodeArray.title) {
		$('#title').html(nodeArray.title);
		$('#dates').html(nodeArray.dates.substring(0, 10));
		$('#content').html(nodeArray.content);
		$('#type').html(nodeArray.type);
		$('#readcount').html(nodeArray.readcount);

		if (nodeArray.picurl != null && nodeArray.videoid == null) {
			//隐藏视频div
			var style = "display: none;"
			var v = document.getElementById("video");
			v.style.display = "none";//隐藏释放页面空间
			$('#image_url').attr('src', nodeArray.picurl);
		}

		//初始化数据 关于页面的尺寸的设置
		var width = window.innerWidth - 32;
		var height = width * 3 / 4;
		//height = height > window.innerHeight ? window.innerHeight : height;
		window.tvpSetting = {
			width : width,
			height : height,
		};
		//生产模式
		var videoid = nodeArray.videoid;
		//调试模式
		//var videoid="l0171ne1fjx";
		if (videoid != null) {
			//隐藏图片div
			var style="display: none;"
			var picture = document.getElementById("picture");
			picture.style.display="none";//隐藏释放页面空间
			//var videoid = nodeArray.videoid;
			//创建 tvp.VideoInfo 类（待播放的视频信息）的对象实例。
			var video = new tvp.VideoInfo();
			//视频vid，从腾讯视频V+上传得到。（点播视频）
			video.setVid(videoid);
			//创建 tvp.Player 类（播放器信息）的对象实例。
			var player =new tvp.Player();
			//默认封面图
			player.create({
				 width  : tvpSetting.width,
				  height : tvpSetting.height,
				  pic    : tvp.common.getVideoSnapMobile(videoid),
				  video  : video,
				  modId  : 'video',
				  isContinuePlay: false,
				  autoplay: false,
				  playerType : 'html5',
				  isHtml5UseFakeFullScreen : false,
				onplay: function () {
					if(os=='android'&& window.tkInterface!=undefined){
						player.pause();
						if ($('#android_video').html() == "") {
							$('#android_video').html($('#video').html());
							$('#android_video').find('.tvp_container').html("");
							$('#android_video').find('.tvp_container').append($('#video').find('.tvp_overlay_poster')[0]);
							$('#android_video').append('<div class="tvp_overlay_play"><span class="tvp_button_play"></span></div>');
							$('#android_video').find('.tvp_button_play').click(function(event) {
								callAndroidJs(video);
							});
							/*$('#android_video').attr('style', $('#android_video').attr('style')+$('#video').find('.tenvideo_player').attr('style'));
							$('#android_video').append('<div class="tvp_video"></div>');
							$('#android_video').find('.tvp_video').append('<img width="100%" height="100%" src="' + tvp.common.getVideoSnapMobile(videoid) + '" />');
							$('#android_video').find('.tvp_video').append('<div class="tvp_shadow"></div>');
							$('#android_video').append('<div class="tvp_overlay_play"><span class="tvp_button_play"></span></div>');
							$('#android_video').find('.tvp_button_play').click(function(event) {
								callAndroidJs(video);
							});*/
						}
						$('#video').hide();
						$('#android_video').show();
					}
		        },
		        onplaying: function () {
		        	if(os=='android'&& window.tkInterface!=undefined){
		        		player.pause();
		        	}
		        },
		        onresume: function () {
		        	if(os=='android'&& window.tkInterface!=undefined){
		        		player.pause();
		        	}
		        }
				});
		}
	}	
}

function callAndroidJs(video) {
	window.tkInterface.invokeMethod(video.url);
}
//获取url参数
function getParameter(param) {
	var query = window.location.search;
	var iLen = param.length;
	var iStart = query.indexOf(param);
	if (iStart == -1)
		return "";
	iStart += iLen + 1;
	var iEnd = query.indexOf("&", iStart);
	if (iEnd == -1)
		return query.substring(iStart);
	return query.substring(iStart, iEnd);
}